package itcast.dao.impl;

import itcast.dao.Drom_manageDao;
import itcast.domain.Build;
import itcast.domain.Buildmanager;
import itcast.domain.Drom;
import itcast.domain.Studnet;
import itcast.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Drom_manageDaoImpl implements Drom_manageDao {
    /**
     * 申明JdbcTemplate类,出入连接池
     */
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());

    //查找数据库信息

    @Override
    public List<Drom> findAll() {
        //使用jdbc操作数据库
        //1.定义sql
        String sql="select * from drom";
        //query返回list集合，BeanPropertyRowMapper指定字节码
        List<Drom>  droms=template.query(sql,new BeanPropertyRowMapper<Drom>(Drom.class));

        return droms;
    }

    //添加数据库信息
    @Override
    public void addDrom(Drom drom) {


        //1.定义sql语句
        String sql="insert into drom(drom_buildid,drom_name,drom_type,drom_number,drom_tel) values(?,?,?,?,?)";
        //2.执行sql
        template.update(sql,drom.getDrom_buildid(),drom.getDrom_name(),drom.getDrom_type(),
                drom.getDrom_number(),drom.getDrom_tel());
    }

    //删除数据库信息

    @Override
    public void delete(int id) {
        //1.定义sql
        String sql="delete from drom where Drom_ID=?";
        //2.执行sql
        template.update(sql,id);
    }
    //根据id找到数据库数据

    @Override
    public Drom findByID(int id) {
        //1.定义sql语句
        String sql="select * from drom where Drom_ID=?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<Drom>(Drom.class),id);

    }

    //修改数据库信息
    @Override
    public void update(Drom drom) {
        //1.定义sql语句
        String sql="update drom set Drom_BuildID=?, Drom_Name=?,Drom_Type=?,Drom_Number=?,Drom_Tel=?" +
                "where Drom_ID=?";
        //执行sql语句
        template.update(sql,drom.getDrom_buildid(),drom.getDrom_name(),drom.getDrom_type(),
                drom.getDrom_number(),drom.getDrom_tel(),drom.getDrom_id());
    }
    //条件搜索数据库

    @Override
    public List<Drom> finddromByDate(Map<String, String[]> condition) {
        //1.定义模板初始化sql
        String sql="select * from drom where 1=1";
        StringBuilder sb=new StringBuilder(sql);
        //2.遍历map中value是否有值
        Set<String> keySet =condition.keySet();
        //定义参数集合
        List<Object> params=new ArrayList<Object>();
        for(String key :keySet){
            //获取value
            String value=condition.get(key)[0];
            //判断value是否有值
            if(value!=null && !"".equals(value)){
                sb.append(" and "+key+" like ? ");
                params.add("%"+value+"%");//条件的值
            }
        }
        System.out.println(sb.toString());
        System.out.println(params);
        List<Drom> droms=template.query(sb.toString(),new BeanPropertyRowMapper<Drom>(Drom.class),params.toArray());
        return droms;
    }

    @Override
    public List<Studnet> finddrom_exchangeByDate(Map<String, String[]> condition) {
        //1.定义模板初始化sql
        String sql="select * from student where Student_Name=?";

        List<Studnet> studnets=template.query(sql,new BeanPropertyRowMapper<Studnet>(Studnet.class),condition);
        return studnets;
    }
}

